function DepotRefresh() {

    if ($('#tree').children('img#repositoryThrobber').size() == 0) {
        loading();
        $.ajax({
            url: "/Repository", 
            type: 'GET',
            dataType: 'json',
            data: {},
            success: function (myData) {
                showDepot(myData);
            },
            error: function () {
                showError();
            }
        });
    }
}

function showDepot(myData) {
    $("#tree").jstree({
        "json_data": {
            "data": myData
        },
        "themes": {
            "theme": "classic",
            "dots": true,
            "icons": true
        },
        "plugins": ["themes", "json_data", "ui"]
    }).bind("select_node.jstree", function (e, data) {
        var node = $.jstree._focused().get_selected();
        if (($.jstree._focused().get_text(node)).match(".txt$")) {
            getContentOfFile($.jstree._focused().get_text(node), $(data.rslt.obj).data('href'));
        }
    });
}

function loading() {
    $("#tree").empty().html('<img id="repositoryThrobber" src="Content/images/throbber.gif" /><p>Repository is loading, please wait, it may take a few minutes.</p>');
}

function showError() {
    $("#tree").empty().html('<p id="error">Fail loading the repository</p>');
}

function getContentOfFile(title, path) {
    $.ajax({
        url: "/Repository/getContentOfFile",
        type: 'GET',
        dataType: 'text',
        data: {
            'path' : path
        },
        success: function (myData) {
            displayTxt(title, myData);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}

function displayTxt(title, text) {
    $("#view").empty().html('<p>' + text + '</p>');
    $("#view").dialog({ height: 500, width: 778, title: title, show: { effect: 'slide', direction: "left" }, hide: { effect: 'slide', direction: "right"}, resizable: false, draggable : false});
}
